package org.zerofwk.example.datastruct.sparsearray;

import java.io.*;

/**
 * desc:
 * 稀疏数组的运用
 *
 * @author: Administrator
 * @date: 2021/3/30 17:55
 */
public class SparseArray {
    public static void main(String[] args) {
        int[][] arr1 = new int[7][11];
        arr1[2][3] = 1;
        arr1[3][4] = 1;
        arr1[1][5] = 3;
        System.out.println("二维数组");
        sprint(arr1);

        int sum = 0;
        for (int[] arr : arr1) {
            for (int data : arr) {
                if (data > 0) {
                    sum++;
                }
            }
        }

        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = arr1.length;
        sparseArr[0][1] = arr1[0].length;
        sparseArr[0][2] = sum;
        System.out.println("\n新的二维数组");

        int count = 0;
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                if (arr1[i][j] > 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = arr1[i][j];
                }
            }
        }
        sprint(sparseArr);

        System.out.println("\n还原二维数组");
        int[][] newArr1 = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr.length; i++) {
            newArr1[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        sprint(newArr1);

        File newFile = new File("E:\\projects\\projects_study\\zero-example-datastruct\\sparseArr.txt");
        File newFile2 = new File("E:\\projects\\projects_study\\zero-example-datastruct\\sparseArr.txt");

//        System.out.println("\n文件读出的二维数组");
//        try {
//            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//            BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
//            for (int[] arr : sparseArr) {
//                bw.write(String.valueOf(arr[0]));
//                bw.write("\t");
//                bw.write(String.valueOf(arr[1]));
//                bw.write("\t");
//                bw.write(String.valueOf(arr[2]));
//                bw.write("\t");
//                bw.write("\n");
//            }
//            bw.close();
//
//            System.out.println("\n");
//            BufferedReader br = new BufferedReader(new FileReader(newFile2));
//            String line = "";
//            while ((line = br.readLine()) != null) {
//                System.out.println(line);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        System.out.println("\n文件读出的二维数组");
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
            for (int[] ints : sparseArr) {
                bos.write(ints[0]);
                bos.write(ints[1]);
                bos.write(ints[2]);
                bos.write('^');
            }
            bos.close();

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(newFile2));
            int newInt = 0;
            while ((newInt = bis.read()) > 0) {
                if ((char) newInt == '^') {
                    System.out.println("");
                } else {
                    System.out.printf("%d\t", newInt);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void sprint(int[][] arr1) {
        for (int[] arr : arr1) {
            for (int data : arr) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }
}
